import {request} from '@inbiz/utils';
const version = InbizConfig.API_VERSION

export async function getModelengine(appid: string, params: any = {}) {
  return request(`/inbiz/api/services/modelengine/v${version}/model/manys/${appid}`, {
    method: 'get',
    params,
  });
}

export async function getModeFields(appid: string, params: any = {}) {
  var modelKeysStr = params.modelKeys;
  var modelKeys = modelKeysStr.split('&modelKeys=');
  return request(`/inbiz/api/services/modelengine/v${InbizConfig.API_VERSION}/model/manyFields`, {
    method: 'post',
    data: {
      ...params,
      modelKeys,
      appId: appid,
    },
  });
}

export async function getTreeDatas(appid: string, params: any = {}) {
  if (params.key) {
    params.value = params.key;
  }
  return request(`/inbiz/api/services/front/modelengine/v${version}/assembly/treeDatas/${appid}`, {
    method: 'get',
    params: params,
  });
}

export async function getControlField(params: any = {}) {
  return request(`/inbiz/api/services/modelengine/v${version}/modelQueryAdapter/controlField`, {
    method: 'get',
    params: params,
  });
}

export async function delTreeData(appid: string, params: any = {}):Promise<boolean | {
  result: number
  message: string
}> {
  return request(`/inbiz/api/services/front/modelengine/v${version}/assembly/TreeData/${appid}`, {
    method: 'delete',
    params: params,
  });
}

export async function queryTreeParentIdDatas(
  appid: string,
  params: { sourceKey: string; sourceType: 1; id: string; parentId: string; values: string },
) {
  return request(
    `/inbiz/api/services/front/modelengine/v${version}/assembly/TreeParentIdDatas/${appid}`,
    {
      method: 'get',
      params: params,
    },
  );
}
